﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
namespace LeagueUtils
{

    /*
     * This is the preferences class that handles the various data needed by league Tools such as the
     * league directory and the UI scales (for later use). If the file doesnt exist yet, it is created.
     */
    public class Preferences
    {

        public static string leagueFolder = "C:\\Games\\League of Legends\\Riot Games\\League of Legends";//"C:\\Riot Games\\League of Legends";
        public static bool disclaimerAccepted = false;
        public static bool hideLeagueBorders = true;
        public static int processingTimer = 50;
        public static int UITimer = 32;
        public static int GameWindowHeight = 768;
        public static int GameWindowWidth = 1024;
        public static double GameGlobalScale = 1.0000;

        public static bool Load()
        {
            if (!File.Exists("preferences.xml"))
            {
                //create everything

                Save();
                return false;
            }
            //load anything that is needed
            XmlDocument pref = new XmlDocument();
            int optionCount = 0;
            pref.Load("preferences.xml");
            if (pref["Preferences"]["LeagueFolder"] != null)
            {
                leagueFolder = pref["Preferences"]["LeagueFolder"].InnerText;
                optionCount++;
            }
            if (pref["Preferences"]["DisclaimerAccepted"] != null)
            {
                disclaimerAccepted = Convert.ToBoolean(pref["Preferences"]["DisclaimerAccepted"].InnerText);
                optionCount++;
            }
            if (pref["Preferences"]["HideLeagueBorders"] != null)
            {
                hideLeagueBorders = Convert.ToBoolean(pref["Preferences"]["HideLeagueBorders"].InnerText);
                optionCount++;
            }
            if (pref["Preferences"]["ProcessingTimer"] != null)
            {
                processingTimer = Convert.ToInt32(pref["Preferences"]["ProcessingTimer"].InnerText);
                optionCount++;
            }
            if (pref["Preferences"]["UITimer"] != null)
            {
                UITimer = Convert.ToInt32(pref["Preferences"]["UITimer"].InnerText);
                optionCount++;
            }
            if (File.Exists(FileFinder.findCfgFile()))
            {
                StreamReader sr = new StreamReader(FileFinder.findCfgFile());
                string line = sr.ReadLine();
                while (line != null)
                {
                    if (line.ToLower().Contains("height"))
                    {
                        GameWindowHeight = int.Parse(line.Substring(line.IndexOf("=")+1));
                        optionCount++;
                    }
                    if (line.ToLower().Contains("width"))
                    {
                        GameWindowWidth = int.Parse(line.Substring(line.IndexOf("=")+1));
                        optionCount++;
                    }
                    line = sr.ReadLine();
                }
            }

            if (File.Exists(FileFinder.findHUDFile("hud" + GameWindowWidth + "x" + GameWindowHeight + ".ini")))
            {
                StreamReader sr = new StreamReader(FileFinder.findHUDFile("hud" + GameWindowWidth + "x" + GameWindowHeight + ".ini"));
                string line = sr.ReadLine();
                while (line != null)
                {
                    if (line.ToLower().Contains("globalscale"))
                    {
                        GameGlobalScale = double.Parse(line.Substring(line.IndexOf("=") + 1));
                        optionCount++;
                        break;
                    }
                    line = sr.ReadLine();
                }
            }
            if (optionCount != 7)
            {
                Save();
            }
            return true;
        }

        public static void Save()
        {
            XmlDocument xd = new XmlDocument();

            xd.LoadXml("<Preferences><LeagueFolder>" + leagueFolder + 
                "</LeagueFolder><DisclaimerAccepted>" + disclaimerAccepted + 
                "</DisclaimerAccepted><HideLeagueBorders>" + hideLeagueBorders + 
                "</HideLeagueBorders><ProcessingTimer>" + processingTimer +
                "</ProcessingTimer><UITimer>" + UITimer +
                "</UITimer><GameWindowWidth>" + GameWindowWidth +
                "</GameWindowWidth><GameWindowHeight>" + GameWindowHeight +
                "</GameWindowHeight><GameGlobalScale>" + GameGlobalScale +
                "</GameGlobalScale></Preferences>");
            xd.Save("preferences.xml");
        }

    }
}
